﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using MartinsEssentials.MVVM;
using MALRECrewrite.ViewModels;
using System.Xml.Serialization;
using System.Linq;

namespace MALRECrewrite.Models
{
    public class Anime : ViewModelBase
    {
        public Anime()
        {
            CheckForSequels = true;
        }

        public bool DataDownloaded { get; set; }

        public int Aid { get; set; }

        public string Title { get; set; }

        public string Uri { get; set; }

        public int Score { get; set; }

        private ObservableCollection<Recommendation> _Recommendations = new ObservableCollection<Recommendation>();

        public ObservableCollection<Recommendation> Recommendations
        {
            get
            {
                return _Recommendations;
            }
            set
            {
                _Recommendations = value;
            }
        }

        private bool _IsActive;

        public bool IsActive
        {
            get
            {
                return _IsActive;
            }
            set
            {
                if (value != _IsActive)
                {
                    _IsActive = value;
                    OnPropertyChanged("IsActive");
                }
            }
        }

        private bool _IsDropped;

        public bool IsDropped
        {
            get
            {
                return _IsDropped;
            }
            set
            {
                if (value != _IsDropped)
                {
                    _IsDropped = value;
                    OnPropertyChanged("IsDropped");

                    CheckForSequels = !IsDropped;
                }
            }
        }

        public bool CheckForSequels { get; set; }

        public bool IsSequel { get; set; }

        private List<int> _ChildElementAids = new List<int>();

        public List<int> ChildElementAids
        {
            get { return _ChildElementAids; }
            set 
            {
                _ChildElementAids = value;

                if (_ChildElementAids.Count > 0)
                {
                    Children.Clear();
                    foreach (int aid in _ChildElementAids)
                    {
                        Anime a = App.DataModel.Animes.Where(x => x.Aid == aid).FirstOrDefault();
                        if (a != null)
                        {
                            Children.Add(a);
                            a.IsSequel = true;
                        }
                    }
                }
            }
        }

        [XmlIgnore]
        private ObservableCollection<Anime> _Children = new ObservableCollection<Anime>();
        [XmlIgnore]
        public ObservableCollection<Anime> Children
        {
            get
            {
                return _Children;
            }
            set
            {
                if (value != _Children)
                {
                    _Children = value;
                    OnPropertyChanged("Children");
                }
            }
        }
    }
}